const app = getApp()
const db = wx.cloud.database()
var refreshTime = 0
var imgPrefix = 'cloud://drawu-6gmzyvz5d9ee56aa.6472-drawu-6gmzyvz5d9ee56aa-1316724426/img/'
var bgArr = []

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bgArr: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                active: 1
            })
        }
        this.getGame(refreshTime)
        this.setData({
            search: this.searchGame.bind(this),
        }) 
    },
    getUserGame(){
        db.collection("userDB").where({
            _openid : app.globalData.openid
        }).get({
            success: (res) => {
                let games = Object.keys(res.data[0].games)
                let bgArr = this.data.bgArr
                bgArr.forEach((item,index) => {
                    if (games.includes(item._id)) {
                        this.setData({[`bgArr[`+index+`].selected`]: true})
                    }
                })
            }
        })
    },

    getGame(times) {
        db.collection("bgDB").get({
            success: (res) => {  
                refreshTime += 1
                let bgArr = res.data
                console.log(res);
                this.setData({bgArr},(res) => {this.getUserGame()})
            }
        })
    },

    searchGame: function (value) {
        let that = this
        bgArr = []
        console.log(value);
        db.collection('bgDB').where({
            name: db.RegExp({
                regexp: value + '+',
                options: 'i',
            })
        }).get({  
            success: function(res) {
                for (let i = 0 ; i < res.data.length ; i++) {
                    let bg = {
                        id: res.data[i]._id,
                        name: res.data[i].name,
                        image: imgPrefix + res.data[i].image, 
                        time: res.data[i].time,
                        difficulty: res.data[i].difficulty,
                        players: res.data[i].players,
                        id: res.data[i]._id,
                        recommendation: res.data[i].recommendation,
                        types:res.data[i].types
                    }
                    bgArr.push(bg)
                  }
                  console.log(bgArr);
                  that.setData({bgArr}) 
                // return new Promise((resolve, reject) => {
                //     resolve()
                // })
            }
        })
    },
    
    searchBarOnFocus(e) {
        this.setData({
            searchBarOnFocus: true
        })
    },
    searchBarOnBlur(e) {
        this.setData({
            searchBarOnFocus: false
        })
    },
    gotoBoardGame(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../BoardGame/BoardGame',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{id: id})
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    backToTop() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        bgArr = []
        refreshTime = 0
        this.getGame(refreshTime)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // this.getGame(refreshTime)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})